<?php

class ReportsController extends AppController
{
    # Controller Name to be used
    var $name = 'Reports';

    # Models to be used
    var $uses = array(
        'User','Project','Branch','Department','Ticket'
    );

    # set the layout to use
    var $layout = 'report_layout';

    function  beforeFilter(){
        parent::beforeFilter();
    }



    function index(){
        $start_dt = date('Y-m-1');
        $end_dt = date('Y-m-t');
        $user = null;
        $default_user = 'all';
        $user_name = '';
        $priority = null;
        $default_priority = 'all';
        $priority_name='';
        $project = null;
        $default_project = 'all';
        $project_name='';
        $department = null;
        $default_department = 'all';
        $department_name='';
        $branch = null;
        $default_branch = 'all';
        $branch_name='';

        if($this->request->is('post')){
            $start_dt = $this->request->data['Query']['start_dt'];
            $end_dt = $this->request->data['Query']['end_dt'];
            if($this->request->data['Query']['user'] != 'all'){
                $user = $this->request->data['Query']['user'];
                $default_user = $this->request->data['Query']['user'];
                $user_name = 'By '.$this->request->data['Query']['user_name'];
            }
            if($this->request->data['Query']['priority'] != 'all'){
                $priority = $this->request->data['Query']['priority'];
                $default_priority = $this->request->data['Query']['priority'];
                $priority_name = $this->request->data['Query']['priority_name'];
            }
            if($this->request->data['Query']['project'] != 'all'){
                $project = $this->request->data['Query']['project'];
                $default_project = $this->request->data['Query']['project'];
                $project_name = $this->request->data['Query']['project_name'];
            }
            if($this->request->data['Query']['department'] != 'all'){
                $department = $this->request->data['Query']['department'];
                $default_department = $this->request->data['Query']['department'];
                $department_name = $this->request->data['Query']['department_name'];
            }
            if($this->request->data['Query']['branch'] != 'all'){
                $branch = $this->request->data['Query']['branch'];
                $default_branch = $this->request->data['Query']['branch'];
                $branch_name = $this->request->data['Query']['branch_name'];
            }
        }

        $g_data = $this->getTicketConsolidated($start_dt,$end_dt,$user,$priority,$project,$department,$branch);

        $graph_title = 'Monthly Consolidated Chart ';//.$product_name;
        $table_title = 'Monthly Consolidated Table ';//.$product_name;

        $user_list_data = $this->getGetUserList();
        $user_list = array('all'=>'All');
        foreach($user_list_data as $dat){
            $user_list[$dat['id']]=$dat['name'];
        }

        $projects_data = $this->getProjects();
        $projects = array('all'=>'All');
        foreach($projects_data as $dat){
            $projects[$dat['id']]=$dat['name'];
        }

        $priority_data = $this->getPriorities();
        $priorities = array('all'=>'All');
        foreach($priority_data as $dat){
            $priorities[$dat['id']]=$dat['name'];
        }

        $department_data = $this->getDepartments();
        $departments = array('all'=>'All');
        foreach($department_data as $dat){
            $departments[$dat['id']]=$dat['name'];
        }

        $branch_data = $this->getBranches();
        $branches = array('all'=>'All');
        foreach($branch_data as $dat){
            $branches[$dat['id']]=$dat['name'];
        }

        $title_for_layout = 'Consolidated Tickets Report';
        $page_description = 'Consolidated  Tickets Report';
        $this->set(compact('title_for_layout','page_description','graph_title','table_title','g_data','user_list','projects','priorities','departments','branches','default_user','default_priority','default_project','default_department','default_branch','start_dt','end_dt'));
    }

    function print_export_consolidated(){
        $download = false;

        $media_type = $_POST['data_type'];
        $start_dt = $_POST['data_start_dt'];
        $end_dt = $_POST['data_end_dt'];

        $user = $_POST['data_user'];
        $user_name = $_POST['data_user_name'];
        if($user == 'all'){
            $user = null;
            $user_name = '';
        }
        $priority = $_POST['data_priority'];
        $priority_name=$_POST['data_priority_name'];
        if($priority == 'all'){
            $priority = null;
            $priority_name = '';
        }
        $project = $_POST['data_project'];
        $project_name=$_POST['data_project_name'];
        if($project == 'all'){
            $project = null;
            $project_name = '';
        }
        $department = $_POST['data_department'];
        $department_name=$_POST['data_department_name'];
        if($department == 'all'){
            $department = null;
            $department_name = '';
        }
        $branch = $_POST['data_branch'];
        $branch_name=$_POST['data_branch_name'];
        if($branch == 'all'){
            $branch = null;
            $branch_name = '';
        }

        $g_data = $this->getTicketConsolidated($start_dt,$end_dt,$user,$priority,$project,$department,$branch);

        $export_title = " Consolidated Orders ";
        $table_title = " Consolidated Orders ";

        $this->autoLayout = false;

        if($media_type == 'export'){
            $list_headers = $g_data['table']['thead'];
            $list_data = $g_data['table']['tbody'];
            if(!empty($list_data)){
                $download = true;
            }
            $filename = $export_title;
            $res = $this->convertToExcel($list_headers,$list_data,$filename);
            $objPHPExcel = $res['excel_obj'];
            $filename = $res['filename'];
        }
        elseif($media_type == 'print'){
            $this->autoLayout = true;
            $this->layout = 'print_layout';
        }
        $print_title = $table_title;
        $controller = $this;

        $this->set(compact('controller','company_profile','print_title','table_title','objPHPExcel', 'download', 'filename','media_type','g_data'));
    }


    function user_tickets(){
        $start_dt = date('Y-m-1');
        $end_dt = date('Y-m-t');
        $ticket = null;
        $default_ticket = '';
        $user = null;
        $default_user = 'all';
       // $user_name = '';
        $status = null;
        $default_status = 'all';
       // $status_name = '';
        $priority = null;
        $default_priority = 'all';
       // $priority_name='';
        $project = null;
        $default_project = 'all';
       // $project_name='';
        $department = null;
        $default_department = 'all';
       // $department_name='';
        $branch = null;
        $default_branch = 'all';
       // $branch_name='';

        if($this->request->is('post')){
            $start_dt = $this->request->data['Query']['start_dt'];
            $end_dt = $this->request->data['Query']['end_dt'];
            if(trim($this->request->data['Query']['ticket']) != ''){
                $ticket = $this->request->data['Query']['ticket'];
                $default_ticket = $this->request->data['Query']['ticket'];
            }

            if($this->request->data['Query']['status'] != 'all'){
                $status = $this->request->data['Query']['status'];
                $default_status = $this->request->data['Query']['status'];
               // $status_name = 'By '.$this->request->data['Query']['status_name'];
            }
            if($this->request->data['Query']['user'] != 'all'){
                $user = $this->request->data['Query']['user'];
                $default_user = $this->request->data['Query']['user'];
               // $user_name = 'By '.$this->request->data['Query']['user_name'];
            }
            if($this->request->data['Query']['priority'] != 'all'){
                $priority = $this->request->data['Query']['priority'];
                $default_priority = $this->request->data['Query']['priority'];
               // $priority_name = $this->request->data['Query']['priority_name'];
            }
            if($this->request->data['Query']['project'] != 'all'){
                $project = $this->request->data['Query']['project'];
                $default_project = $this->request->data['Query']['project'];
               // $project_name = $this->request->data['Query']['project_name'];
            }
            if($this->request->data['Query']['department'] != 'all'){
                $department = $this->request->data['Query']['department'];
                $default_department = $this->request->data['Query']['department'];
                //$department_name = $this->request->data['Query']['department_name'];
            }
            if($this->request->data['Query']['branch'] != 'all'){
                $branch = $this->request->data['Query']['branch'];
                $default_branch = $this->request->data['Query']['branch'];
                //$branch_name = $this->request->data['Query']['branch_name'];
            }
        }

        $g_data = $this->getTicketReport($start_dt,$end_dt,$ticket,$user,$status,$priority,$project,$department,$branch);

        $table_title = 'Ticket List';

        $user_list_data = $this->getGetUserList();
        $user_list = array('all'=>'All');
        foreach($user_list_data as $dat){
            $user_list[$dat['id']]=$dat['name'];
        }

        $projects_data = $this->getProjects();
        $projects = array('all'=>'All');
        foreach($projects_data as $dat){
            $projects[$dat['id']]=$dat['name'];
        }

        $priority_data = $this->getPriorities();
        $priorities = array('all'=>'All');
        foreach($priority_data as $dat){
            $priorities[$dat['id']]=$dat['name'];
        }

        $department_data = $this->getDepartments();
        $departments = array('all'=>'All');
        foreach($department_data as $dat){
            $departments[$dat['id']]=$dat['name'];
        }

        $branch_data = $this->getBranches();
        $branches = array('all'=>'All');
        foreach($branch_data as $dat){
            $branches[$dat['id']]=$dat['name'];
        }

        $statuses_data = $this->getStatus();
        $statuses = array('all'=>'All');
        foreach($statuses_data as $dat){
            $statuses[$dat['id']]=$dat['name'];
        }

        $title_for_layout = 'User Ticket Report';
        $page_description = 'User Ticket Report';
        $this->set(compact('title_for_layout','page_description','table_title','g_data','user_list','projects','priorities','departments','branches','statuses','default_user','default_priority','default_project','default_department','default_branch','default_ticket','default_status','start_dt','end_dt'));
    }
}